package com.android.duininsuoai.fragment.group;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.duininsuoai.R;
import com.android.duininsuoai.fragment.BaseLazyFragment;
import com.android.duininsuoai.utils.MyLog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/26.
 */

public class Fragment_NewerGuideWeb extends BaseLazyFragment {


    @BindView(R.id.webView)
    WebView webView1;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.web_nodata)
    TextView nodata;
    private FragmentActivity mcontext;
    private View view;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String webUrl;
    private String detail_content;


    public Fragment_NewerGuideWeb() {
    }





    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static Fragment_NewerGuideWeb newInstance(String param1, String web_content) {
        Fragment_NewerGuideWeb fragment = new Fragment_NewerGuideWeb();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, web_content);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            webUrl = getArguments().getString(ARG_PARAM1);
            detail_content = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    private void initUI() {
        WebSettings settings = webView1.getSettings();
        settings.setJavaScriptEnabled(true);
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {
        if (webUrl.equals("")) {
            if (detail_content != null && !detail_content.equals("")) {
                progress.setVisibility(View.GONE);
                nodata.setVisibility(View.GONE);
                webView1.setWebViewClient(new MyWebViewClient());
                webView1.setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onProgressChanged(WebView view, int newProgress) {
                        super.onProgressChanged(view, newProgress);
                        if (newProgress == 100) {
                            progress.setVisibility(View.GONE);
                        }
                    }
                });
                //宽度适应屏幕
                String js = "<style>\n" +
                        "img{\n" +
                        " max-width:100%;\n" +
                        "height:auto\n" +
                        "}\n" +
                        "</style>" + detail_content;

                webView1.loadDataWithBaseURL(null, js, "text/html", "utf-8", null);
            } else {
                progress.setVisibility(View.GONE);
                nodata.setVisibility(View.VISIBLE);
            }
        } else {
            MyLog.e("weburl",webUrl);
            webView1.setWebViewClient(new MyWebViewClient());
            if (!"".equals(webUrl)) {
                webView1.loadUrl(webUrl);
            }
        }


    }


    class MyWebViewClient extends WebViewClient {
        //拦截网址
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_detail_goods_detail_web, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }


}
